<?php

/**
 * Created by PhpStorm.
 * User: luzhaoyang
 * Date: 2017/6/2
 * Time: 17:03
 */
class Commqpass extends Base_Controller
{
    public function __construct()
    {
        $this->_page_size = 20;
        $this->_form_type = 'commqpass';
        $this->_list_where = array('q.iscontrolled'=>1);
        $this->_action_list = array();
        $this->_controller = 'admin/commqpass/';//admin/user/
        parent::__construct();
    }

    public function showList($page = 1)
    {
        $this->_form_type = 'commq';
        $this->setFormtable();
        $limit = $this->_page_size;
        $offset = ($page-1) * $limit;

        $get = $this->input->get();
        if (!empty($get)){
            $where = array();
            $where['q.iscontrolled']=1;
            $start = !empty($get['start'])?strtotime(trim($get['start'])):'';
            $end = !empty($get['end'])?strtotime(trim($get['end'])):'';
            $user = !empty($get['fusername'])?trim($get['fusername']):'';
            $question = !empty($get['fquestion'])?trim($get['fquestion']):'';
            if (!empty($start)) $where['q.changed >'] = $start;
            if (!empty($end)) $where['q.changed <'] = $end;
            if (!empty($user)) $where['u.userName LIKE']= '%'.$user.'%';
            if (!empty($question)) $where['q.question LIKE']= '%'.$question.'%';
            $this->_list_where = $where;
            $filter = $this->load->view('admin/form/filter/qlistfilter',array('controller'=>site_url($this->_controller),
                'start'=>$get['start'],'end'=>$get['end'],'fusername'=>$user,'fquestion'=>$question),true);
        }else{
            $filter = $this->load->view('admin/form/filter/qlistfilter',array('controller'=>site_url($this->_controller)),true);
        }
        $pdata = $this->getPagination($this->_controller.'showList/');

        $res = $this->getList($offset,$limit);

        $data = array('headers'=>$res['headers'],'list'=>$res['list'],'pagination'=>$pdata['pagination'],'filter'=>$filter);
        $content = $this->load->view('admin/form/list',$data,true);

        echo $this->getAdminView($content);
    }
    //重写获取list数据
    public function getList($offset = 0,$limit = 0)
    {
        $this->db->select('q.id,u.userName,q.question,q.created')->from('comm_question as q')->join('mod_user as u','u.uid = q.uid','left');
        if (!empty($this->_list_where)) $this->db->where($this->_list_where);
        if (!empty($limit)) $this->db->limit($limit,$offset);
        $this->db->order_by('q.created','ASC');
        $data=$this->db->get()->result_array();

        $res = array('headers'=>array(),'list'=>array());
        $this->load->model('redis_model');
        $redis=$this->redis_model->getRedisInstance();
        foreach($data as $row){
            $rowdata = array();
            $rowdata['userName']=$row['userName'];
            $rowdata['question']=$row['question'];
            $rowdata['read']=$redis->bitCount('question:'.$row['id'].'.viewsraw');
            $rowdata['created']=date('Y-m-d H:i:s',$row['created']);
            $rowdata['actionlist']='<a class="other-btn action-btn" href="'.site_url('admin/commqpass/').
                'editqpass/'.$row['id'].'">控制</a>';
            $rowdata['actionlist'].='<a class="other-btn action-btn" href="'.site_url('admin/commqpassuser/').
                'showList/'.$row['id'].'/1">授权</a>';
            $res['list'][]=$rowdata;
        }
        $res['headers']=array('提问者','问题','阅读数','创建时间','操作');
        return $res;
    }
    public function getPagination($uri = '')
    {
        $this->db->from('comm_question as q')->join('mod_user as u','u.uid = q.uid','left');
        if (!empty($this->_list_where)) $this->db->where($this->_list_where);
        $total = $this->db->count_all_results();

        $this->load->library('pagination');
        $config['base_url'] = site_url($uri);
        $config['total_rows'] = $total;
        $config['per_page'] = $this->_page_size;
        $config['suffix'] = '?'.$_SERVER['QUERY_STRING'];
        $config['first_url'] = $config['base_url'].'1?'.$_SERVER['QUERY_STRING'];
        $config['cur_tag_open'] = '<div class="cur-page">';
        $config['cur_tag_close'] = '</div>';
        $config['use_page_numbers'] = TRUE;
        $config['num_tag_open'] = '<div class="pageitem">';
        $config['num_tag_close'] = '</div>';
        $config['prev_link'] = '上一页';
        $config['prev_tag_open'] = '<div class="pre-page">';
        $config['prev_tag_close'] = '</div>';
        $config['next_link'] = '下一页';
        $config['next_tag_open'] = '<div class="next-page">';
        $config['next_tag_close'] = '</div>';
        $config['last_link'] = '最后一页';
        $config['last_tag_open'] = '<div class="last-page">';
        $config['last_tag_close'] = '</div>';
        $config['first_link'] = '第一页';
        $config['first_tag_open'] = '<div class="first-page">';
        $config['first_tag_close'] = '</div>';
        $config['full_tag_open'] = '<div class="page-total-results">共'.$total.'条，每页'.$this->_page_size.'条</div>';

        $this->pagination->initialize($config);

        return array('total'=>$total,'pagination'=>$this->pagination->create_links());
    }

    public function update($id = 0){echo 'invalid request';}
    public function afterupdate($pkey,$pkeyval){echo 'invalid request';}
    public function create(){echo 'invalid request';}
    public function aftercreate($pkeyval){echo 'invalid request';}

    public function editqpass($qid)
    {
        $post = $this->input->post();
        $exist = $this->adminform_model->checkExist('comm_qpass','id',array('qid'=>$qid));
        if(empty($post)){
            //不存在 create
            if (empty($exist)){
                $form = $this->createBuildForm();
                $content = $this->load->view('admin/form/edit',array('controller'=>site_url($this->_controller),'form'=>$form),true);
                echo $this->getAdminView($content);
            }
            //存在 update
            else{
                $form = $this->updateBuildForm($exist['id']);
                $content = $this->load->view('admin/form/edit',array('controller'=>site_url($this->_controller),'form'=>$form),true);
                echo $this->getAdminView($content);
            }
        }else{
            $dbdata = array();
            $dbdata['title'] = trim($post['title']);
            $dbdata['status'] = (int)$post['status'];
            $dbdata['start'] = strtotime($post['start']);
            $dbdata['end'] = strtotime($post['end']);
            $dbdata['qid'] = (int)$qid;
            $oldimgs = $this->adminform_model->getRowData('comm_qpass','cover',array('qid'=>(int)$qid));
            $oldimgs = $oldimgs['cover'];
            $delimgs = $post['del'];
            if (!empty($delimgs)||$delimgs==='0'){
                $delimgs=explode('|',$delimgs);
                $oldimgs = explode('|',$oldimgs);
                foreach ($delimgs as $d){
                    unset($oldimgs[$d]);
                }
                $oldimgs = implode('|',$oldimgs);
            }
            $dbdata['cover'] = $oldimgs;
            $imgs = $this->uploadImg('cover',500);
            if ($imgs['code'] == 1){
                if (!empty($dbdata['cover'])) $dbdata['cover'] .= '|'.implode("|",$imgs['imgurl']);
                else $dbdata['cover'] = implode("|",$imgs['imgurl']);
            }
            //不存在 create
            if (empty($exist)){
                $this->_create_data = $dbdata;
                parent::create();
            }
            //存在 update
            else{
                $this->_update_data = $dbdata;
                parent::update($exist['id']);
            }
            redirect(site_url($this->_controller).'showList');
        }
    }
    public function delete($id = 0,$externalid = 0){}

    public function afterdelete($pkey,$pkeyval){}
}